/**
 * Filename: TargetService.java
 * @author  Firefly
 * @contact postfirefly@163.com
 * @time    2016/3/20 15:43
 */
package com.github.postsanf.yinian.service;

import android.content.Intent;
import com.github.postsanf.yinian.BaseIntentService;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNAlbum;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.entity.YNAlbumResponse;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;

import java.util.LinkedHashMap;
/**
 * 跳转后台服务
 */
public class TargetService extends BaseIntentService {

    public TargetService() {
        super("TargetService");
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            switch(action) {
                case ACTIONs.actionGetTarAlbum:
                    String  ids = intent.getExtras().getString(ACTIONs.aAddData);
                    getOfficialInfo(ids);
                    break;
            }
        }
    }

    private void getOfficialInfo(String ids){
        LinkedHashMap<String,String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_GROUP_ID, ids);
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL)+getString(R.string.ynShowSingleOfficialAlbum))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true){
                    @Override
                    public void onSuccess(String s, Response<String> response){
                        YNAlbumResponse resOfficialGroup = gson.fromJson(s, YNAlbumResponse.class);
                        if (resOfficialGroup!=null && ReqUtils.isSuccess(resOfficialGroup.getCode())){
                            YNAlbum  tarAlbum = resOfficialGroup.getData().get(0);
                            YNSingleton.getInstance().setCurAlbum(tarAlbum);
                            application.setCurGid(tarAlbum.getGroupid());
                            Intent upIntent = new Intent(ACTIONs.actionTarget);
                            sendBroadcast(upIntent);
                        }
                    }
                    @Override
                    public void onFailure(HttpException e, Response<String> response){
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }
}
